"""
Utility classes, primarily just a couple of things for the python logging module.

Copyright (c) 2009 Christopher Hesse
http://code.google.com/p/python-expect/
"""

import logging


class ExpectLoggingFormatter(logging.Formatter):
    """
    Logging formatter to format messages from channels all special-like
    """
    
    def __init__(self):
        format = "%(asctime)s [%(name)s:%(filename)s:%(lineno)d] %(levelname)-8s %(funcName)s(): %(message)s"
        logging.Formatter.__init__(self, fmt=format)
        
    def format(self, record):
        if hasattr(record, 'direction'):
            if record.direction == 'read':
                direction = '  (read)'
            else:
                direction = ' (write)'
        else:
            direction = ''
        
        # if the record has a channel attribute, stick it in with the message
        if hasattr(record, 'channel'):
            record.msg = "{{Channel {0}}}{1} {2}".format(record.channel, direction, record.msg)
                
        return logging.Formatter.format(self, record)
        

class NullHandler(logging.Handler):
    """
    Handler for logging messages to nowhere by default. If the user of the
    library wants to add logging, they can register their own handlers.
    """
    
    def emit(self, record):
        self.format(record) # just so the formatter gets run